let action = "get";

window.onload = function ()
{
    InitTimeDisplay();
    document.getElementById("float-add-button").click();
    let actionValue = GetParams("action");
    switch (actionValue)
    {
        case "del":
            action = "del";
            break;
        case "get":
            action = "get";
            break;
        default:
            action = "get";
            break;
    }
}

function DelInfo()
{
    let infoBox = event.currentTarget.parentNode;
    let infosBox = infoBox.parentNode;
    if (infosBox.getElementsByClassName("cell-info").length < 2)
    {
        return;
    }
    infosBox.removeChild(infoBox);
}

function AddInfo()
{
    let cellInfoBox = document.getElementsByClassName("cell-infos")[0];
    let newInfo = document.createElement("div");
    newInfo.setAttribute("class", "cell-info");
    newInfo.innerHTML = `
        <label class="row-key-label">行键: </label>
        <input placeholder="行键" class="row-key-input" autocomplete="off">
        <input class="all-column" type="checkbox" onchange="OnAllClicked()">
        <span class="all-column-checkmark"></span>
        <label class="column-label">列名: </label>
        <label class="column-all">该行的全部列将被删除</label>
        <input placeholder="列名" class="column-input" autocomplete="off">
        <button class="info-del" onclick="DelInfo()"></button>`;
    cellInfoBox.appendChild(newInfo);
}

function OnAllClicked()
{
    let columnInput = event.currentTarget.parentNode.getElementsByClassName("column-input")[0];
    let columnLabel = event.currentTarget.parentNode.getElementsByClassName("column-label")[0];
    let columnAll = event.currentTarget.parentNode.getElementsByClassName("column-all")[0];
    if(event.currentTarget.checked)
    {
        columnAll.style.display = "inline";
        columnInput.style.display = "none";
        columnLabel.style.display = "none";
    }
    else
    {
        columnAll.style.display = "none";
        columnInput.style.display = "inline";
        columnLabel.style.display = "inline";
    }
}

function Del()
{
    let data =
        {
            tableName: "",
            rows: []
        };
    data.tableName = document.getElementById("tableNameInput").value;
    if (data.tableName === "")
    {
        ShowAlertOverlay("表名不能为空");
        return;
    }
    let infos = document.getElementsByClassName("cell-info");

    for (let i = 0; i < infos.length; i++)
    {
        let infoObj = infos[i];
        let row =
            {
                rowKey: "",
                column: ""
            };
        row.rowKey = infoObj.getElementsByClassName("row-key-input")[0].value;
        if (row.rowKey === "")
        {
            ShowAlertOverlay("行键不能为空");
            return;
        }
        if (infoObj.getElementsByClassName("all-column")[0].checked)
        {
            row.column = "*";
        }
        else
        {
            row.column = infoObj.getElementsByClassName("column-input")[0].value;
            if (row.column === "")
            {
                ShowAlertOverlay("列名不能为空");
                return;
            }
        }
        data.rows.push(row);
    }
    let url = baseUrl + "/manage/" + data.tableName;
    let dataBody = JSON.stringify(data);
    let http = new XMLHttpRequest();
    http.open("DELETE", url);
    http.send(dataBody);
    http.onreadystatechange = function ()
    {
        if(this.readyState === http.DONE)
        {
            let resJson = JSON.parse(this.responseText);
            let displayText = "";
            switch (this.status)
            {
                case 0:
                    ShowAlertOverlay("无法连接到远端服务器!");
                    CloseLoadingOverlay();
                    break;
                case 200:
                    if(resJson.Warning.length !== 0)
                    {
                        for(let i = 0; i< resJson.Warning.length; i++)
                        {
                            displayText += resJson.Warning[i];
                            displayText += "<br/>";
                        }
                        ShowAlertOverlay(displayText);
                    }
                    else
                    {
                        ShowOkOverlay();
                    }
                    CloseLoadingOverlay();
                    break;
                default:
                    displayText += "Code: ";
                    displayText += this.status;
                    displayText += " ";
                    displayText += this.statusText;
                    displayText += "<br/>";
                    for(let i = 0; i< resJson.Warning.length; i++)
                    {
                        displayText += resJson.Warning[i];
                        displayText += "<br/>";
                    }
                    displayText += resJson.Error;
                    displayText += "<br/>";
                    ShowAlertOverlay(displayText);
                    CloseLoadingOverlay();
                    break;
            }
        }
    };
    ShowLoadingOverlay();
}

function test()
{
    switch (action)
    {
        case "del":
            Del();
            break
    }
}